package com.rocksdata.mc.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import com.ctz.commonframe.ui.BaseActivity;
import com.ctz.commonframe.utils.Logger;
import com.ctz.commonframe.utils.SharedPrefrencesUtil;
import com.rocksdata.mc.R;
import com.rocksdata.mc.SharedPreferencesKey;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.business.UserInfoManager;

import org.xutils.view.annotation.ContentView;

/**
 * Created by 陈堂正 on 2017/5/9.
 */
@ContentView(R.layout.activity_welcome)
public class WelcomeActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                final boolean isFirstEnter = (boolean) SharedPrefrencesUtil.get(SharedPreferencesKey.KEY_FIRST_ENTER, true);
                if(isFirstEnter){
                    gotoGuideActivity();
                }else{
                    String userId = UserInfoManager.getCurrentUserId();
                    if(!TextUtils.isEmpty(userId)){
                        if (UserInfoManager.getModifyPasswordFlag()) {
                            BusinessManager.getAccountManager().onLoginSuccess();
                            gotoMainActivity(false);
                        }else{
                            gotoModifyPasswordActivity();
                        }
                    }else{
                        gotoLoginActivity();
                    }
                }
                finish();
            }
        }, 1000);
    }

    private void gotoGuideActivity(){
        startActivity(new Intent(WelcomeActivity.this, GuideActivity.class));
    }

    private void gotoLoginActivity(){
        startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
    }

    private void gotoMainActivity(boolean isVisitor){
        BusinessManager.getAccountManager().onLoginSuccess();
        UserInfoManager.saveIsVisitor(isVisitor);
        startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
    }

    private void gotoModifyPasswordActivity () {
        startActivity(new Intent(WelcomeActivity.this, ModifyPasswordActivity.class));
    }
}
